package com.ks.composestudy.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.ks.composestudy.MainViewModel
import com.ks.composestudy.R
import com.ks.composestudy.ui.data.Chat

@Composable
fun ChatBarTop(chat: Chat, viewModel: MainViewModel) {
    Row(
        Modifier.background(Color.DarkGray),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Icon(
            painterResource(R.drawable.ic_back),
            "返回",
            Modifier.size(32.dp).padding(5.dp).clickable {
                viewModel.endChat()
            },
            tint = Color.White
        )
        Text(
            chat.friend.name,
            Modifier.weight(1f),
            fontSize = 18.sp,
            color = Color.White,
            textAlign = TextAlign.Center
        )
    }
}

@Preview(showBackground = true)
@Composable
fun ChatBarTopPreview() {
    val viewModel: MainViewModel = viewModel()
    viewModel.currentChat?.let { ChatBarTop(it, viewModel) }
}